<template>
	<view>
		<swiper class="swiper" indicator-color='#FFFFFF' indicator-active-color='#007aff' circular
			:indicator-dots="swiper.indicatorDots" :autoplay="swiper.autoplay" :duration="swiper.duration">
			<swiper-item v-for="(item,index) in data.bannerList" :key="index">
				<image :src="item.image_text" mode="aspectFill"></image>
			</swiper-item>
		</swiper>

		<view class="content">
			<scroll-view class="left" scroll-y="true">
				<view class="left_item" :class="item.isok?'select':''" v-for="(item,index) in data.selectList"
					:key="index" @click="changeSelect(item)">
					{{item.name}}
				</view>
			</scroll-view>
			<scroll-view class="right" scroll-y="true">
				<view class="right_item"
					@click="$pageGo(`/pages/knowledge/knowledgedetail?id=${data.selectListInfo.id}`)">
					<image :src="data.selectListInfo.image_text" mode="aspectFill"></image>
					<view class="tip">
						会员免费
					</view>
					<view class="abs">
						<view class="title">
							{{data.selectListInfo.name}}
						</view>
						<view class="flex_jc_sb flex_b">
							<view class="flex f_l">
								<image src="/static/video.png" mode=""></image>
								共{{data.ulist.length}}课时
							</view>
							<view class="btn" v-if="data.selectListInfo.is_pay == 0 && data.level == 0">
								<text>￥</text>{{data.selectListInfo.price}}
							</view>
							<view class="btn1" v-else>
								去观看
							</view>
						</view>
					</view>
				</view>
				<view class="" v-for="(item,index) in data.ulist" :key="index" @click="pageGo(item)">
					<view class="right_item1">
						<view class="image">
							<image :src="item.image_text" mode="aspectFill"></image>
							<view class="abs flex_jc_cent" :style="{background:item.type==1?'rgba(0, 0, 0, 0.3)':''}">
								<!-- <image v-if="item.type == 1" src="/static/video.png" mode=""></image> -->
								<view class="tip">
									会员免费
								</view>
							</view>
						</view>
						<view class="text">
							{{item.title}}<span v-if="item.type == 2">.{{item.file_load_suffix_text}}</span>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		computed,
		getCurrentInstance
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReachBottom,
		onUnload,
		onHide
	} from '@dcloudio/uni-app'
	import {
		getBanner,
		getCourse,
		getCourseList
	} from '@/common/http.api.js';

	const swiper = reactive({
		indicatorDots: true,
		autoplay: true,
		duration: 500
	})

	const data = reactive({
		level:0,
		selectList: [],
		selectListInfo: {},
		params: {
			page: 1,
			limit: 100,
			cate_id: ''
		},
		ulist: [],
		bannerList: []
	})
	onShow(() => {
		data.level = uni.getStorageSync('user').level?uni.getStorageSync('user').level:0
		getBanner({
			type: 2
		}).then(res => {
			data.bannerList = res.data
		})
		getCourse().then(res => {
			res.data.forEach(item => {
				item.isok = false
			})
			data.selectListInfo = res.data[0]
			data.selectListInfo.isok = true
			data.selectList = res.data
			data.params.cate_id = data.selectListInfo.id
			data.ulist = []
			getList()
		})
	})

	function getList() {
		getCourseList(data.params).then(res => {
			data.ulist.push(...res.data)
		})
	}

	function changeSelect(item) {
		data.selectListInfo = item
		data.params.cate_id = item.id
		data.params.page = 1
		data.selectList.forEach(i => {
			i.isok = false
		})
		item.isok = !item.isok
		getCourseList(data.params).then(res => {
			data.ulist = res.data
		})
	}

	function pageGo(item) {

		//没有购买就弹窗
		if (data.selectListInfo.is_pay == 0 && data.level == 0) {
			uni.showModal({
				title: '系统提示',
				content: '购买课程可全部查看！',
				confirmText: '去购买',
				confirmColor: '#0756FF',
				success: (res) => {
					if (res.confirm) {
						uni.navigateTo({
							url: `/pages/knowledge/knowledgedetail?id=${data.selectListInfo.id}`
						})
					} else if (res.cancel) {}
				}
			});
			return
		}
		// 视频跳转 图文跳转
		if (item.type == 2) {
			wx.showLoading({
				title: "资源下载中...",
			});
			wx.downloadFile({
				url: item.file_load_text, //后端返回的pdf路径
				filePath: `${wx.env.USER_DATA_PATH}/${item.title}.${item.file_load_suffix_text}`, //自定义pdf文件名
				success: function(res) {
					const filePath = res.filePath || res.tempFilePath
					console.log(res);
					wx.openDocument({
						showMenu: true, //分享按钮
						filePath: filePath,
						success: function(res) {
							wx.hideLoading();
						}
					})
				}
			})
			// uni.navigateTo({
			// 	url: `/pages/knowledge/textdetail?id=${item.id}`
			// })
		}
		if (item.type == 1) {
			uni.navigateTo({
				url: `/pages/knowledge/knowledgedetail?id=${data.selectListInfo.id}&videoId=${item.id}`
			})
		}
	}
</script>

<style lang="scss">
	page {
		background: linear-gradient(90deg, #E5F8FF 0%, #FFF3F8 100%);
	}


	.swiper {
		width: 100%;
		width: 710rpx;
		height: 280rpx;
		margin: 20rpx auto;
		border-radius: 20rpx;
		image {
			width: 710rpx;
			height: 280rpx;
			border-radius: 20rpx;
		}
	}

	.content {
		height: calc(100vh - 320rpx);
		display: flex;
		width: 730rpx;
		justify-content: space-between;

		.left {
			width: 200rpx;
			height: calc(100vh - 320rpx);
			background: #fff;
			border-radius: 0rpx 20rpx 0rpx 0rpx;
			padding-top: 50rpx;
			box-sizing: border-box;

			.left_item {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #979797;
				text-align: center;
				height: 50rpx;
				margin-bottom: 30rpx;
				line-height: 50rpx;
				border-left: 6rpx solid #fff;
			}

			.select {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 28rpx;
				color: #000000;
				border-left: 6rpx solid #0756FF;
			}
		}

		.right {
			width: 510rpx;
			height: calc(100vh - 320rpx);

			.right_item1 {
				width: 510rpx;
				height: 160rpx;
				margin: 0rpx auto 20rpx;
				display: flex;
				align-items: center;

				.image {
					width: 240rpx;
					height: 160rpx;
					border-radius: 20rpx;
					position: relative;

					image {
						width: 240rpx;
						height: 160rpx;
						border-radius: 20rpx;
					}

					.abs {
						width: 240rpx;
						height: 160rpx;
						border-radius: 20rpx;
						position: absolute;
						top: 0%;
						background: rgba(0, 0, 0, 0.3);

						image {
							width: 50rpx;
							height: 50rpx;
						}
					}

					.tip {
						width: 100rpx;
						height: 40rpx;
						background: linear-gradient(223deg, #FFC393 0%, #FFE9D9 100%);
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						border: 1px solid #F9B781;
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 20rpx;
						color: #8A3E00;
						line-height: 40rpx;
						text-align: center;
						position: absolute;
						left: 10rpx;
						top: 10rpx;
					}
				}

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #000000;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2; //几行就填几
					-webkit-box-orient: vertical;
					margin-left: 10rpx;
				}
			}

			.right_item {
				width: 510rpx;
				height: 320rpx;
				margin: 0rpx auto 20rpx;
				position: relative;
				border-radius: 20rpx;

				image {
					width: 510rpx;
					height: 320rpx;
					border-radius: 20rpx;
				}

				.tip {
					width: 100rpx;
					height: 40rpx;
					background: linear-gradient(223deg, #FFC393 0%, #FFE9D9 100%);
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					border: 1px solid #F9B781;
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 20rpx;
					color: #8A3E00;
					line-height: 40rpx;
					text-align: center;
					position: absolute;
					left: 10rpx;
					top: 10rpx;
				}

				.abs {
					position: absolute;
					bottom: 0%;
					width: 510rpx;
					height: 120rpx;
					border-radius: 0rpx 0rpx 20rpx 20rpx;
					background: rgba(0, 0, 0, 0.7);
					padding: 10rpx 20rpx 0rpx 10rpx;
					box-sizing: border-box;

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #FFFFFF;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.flex_b {
						margin-top: 10rpx;

						.f_l {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 20rpx;
							color: #FFFFFF;

							image {
								width: 20rpx;
								height: 20rpx;
								margin-right: 10rpx;
							}
						}

						.btn {
							height: 48rpx;
							background: #FF2352;
							border-radius: 12rpx 12rpx 12rpx 12rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 800;
							color: #FFFFFF;
							font-size: 32rpx;
							line-height: 48rpx;
							text-align: center;
							padding: 0rpx 10rpx;

							text {
								font-family: PingFang SC, PingFang SC;
								font-weight: 800;
								font-size: 20rpx;
								color: #FFFFFF;
							}

						}

						.btn1 {
							width: 120rpx;
							height: 48rpx;
							border-radius: 12rpx 12rpx 12rpx 12rpx;
							border: 1px solid #FFFFFF;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 26rpx;
							color: #FFFFFF;
							line-height: 48rpx;
							text-align: center;
						}

					}
				}
			}
		}
	}
</style>